@extends('layouts.admin')

@section('content')
<section id="main-content">
<section class="wrapper">
<div class="row">
  <div class="col-lg-12">
      <section class="panel">
          <header class="panel-heading">
              权限列表
          </header>
          
          <div class="panel-body right"  align="right">
            <div class="form-group " align="" >
                  <a class="btn btn-primary settle" name="to" data-href="" href="{{ route('admin.access.permissions.create') }}">新增</a> 
                  <!-- <a  class="btn btn-danger settleAll" name="to" data-href="">删除</a> -->
                  
                </div>
           

        </div>

          <table class="table table-striped table-advance table-hover">
              <thead>
              <tr>
                    <th>
                        <span class="ckbox ckbox-primary">
                            <!-- <input type="checkbox" id="selectall"/> -->
                            <label for="selectall"></label>
                        </span>
                    </th>
                  <th>显示名称[Name]</th>
                  <th>路由[Slug]</th>
                  <th class="hidden-phone"><i class="icon-question-sign"></i>描述[Descrition]</th>
                  <th>图标[Icon]</th>
                  <th>菜单[Menu]</th>
                  <!-- <th>排序[Sort]</th> -->
                  <th></th>
              </tr>
              </thead>
              <tbody>
               @foreach($permissions as $permission)
              <tr>
                    <td>
                        <div class="ckbox ckbox-default">
                           <!--  <input type="checkbox" name="id" id="id-{{ $permission->id }}"
                                   value="{{ $permission->id }}" class="selectall-item"/> -->
                            <label for="id-{{ $permission->id }}"></label>
                            <a href="javascript:;" class="show-sub-permissions"
                               data-id="{{ $permission['id'] }}"><span
                                        class="glyphicon glyphicon-chevron-right"></span></a>
                        </div>
                    </td>
                    <td><a href="#">{{ $permission->name }}</a></td>
                    <td><a href="#">{{ $permission->slug }}</a></td>
                    <td class="hidden-phone">{{ $permission->description }}</td>
                    <td>{!! $permission->icon_html !!}</td>
                    <td>{!! $permission->is_menu ? '<span class="label label-danger">是</span>':'<span class="label label-default">否</span>' !!}</td>
                    <!-- <td>0</td> -->
                    <td>
            
                        <a href="{{ route('admin.access.permissions.edit',['id'=>$permission->id]) }}" class="btn btn-primary btn-xs"><i class="icon-pencil"></i></a>
                        <a data-href="{{ route('admin.access.permissions.destroy',['id'=>$permission->id]) }}" class="btn btn-danger btn-xs permission-delete"><i class="icon-trash "></i></a>
                    </td>
              </tr>
               @if($permission->sub_permission->count())
                @foreach($permission->sub_permission as $sub)
                <tr class="hide parent-permission-{{ $permission->id }}">
                    <td>
                        <div class="ckbox ckbox-default">
                            <!-- <input type="checkbox" name="id" id="id-{{ $sub->id }}"
                                   value="{{ $sub->id }}" class="selectall-item"/> -->

                            <label for="id-{{ $sub->id }}"></label>
                            
                            <!-- <a href="javascript:;" class="show-sub-permissions"
                               data-id="{{ $sub['id'] }}"><span
                                        class="glyphicon glyphicon-chevron-right"></span></a> -->

                        </div>
                    </td>
                    <td><a href="#">|--{{ $sub->name }}</a></td>
                    <td><a href="#">{{ $sub->slug }}</a></td>
                    <td class="hidden-phone">{{ $sub->description }}</td>
                    <td>{!! $sub->icon_html !!}</td>
                    <td>{!! $sub->is_menu ? '<span class="label label-danger">是</span>':'<span class="label label-default">否</span>' !!}</td>
                    <!-- <td>0</td> -->
                    <td>
                        
                        <a href="{{ route('admin.access.permissions.edit',['id'=>$sub->id]) }}" class="btn btn-primary btn-xs"><i class="icon-pencil"></i></a>
                        <a data-href="{{ route('admin.access.permissions.destroy',['id'=>$sub->id]) }}" class="btn btn-danger btn-xs permission-delete"><i class="icon-trash "></i></a>
                    </td>
                </tr>

                <!--  -->
                
                <!--  -->
                @endforeach
                @endif
               @endforeach
              </tbody>
          </table>
      </section>
  </div>
</div>
  </section>
</section>    
@endsection
@section('javascript')
    <script src="{{ asset('admin/rbacjs/toggles.min.js') }}"></script>
    <script src="{{ asset('admin/js/ajax.js') }}"></script>
    <script>

        $(".show-sub-permissions").toggle(function () {
            var id = $(this).data('id'), subSelector = $('.parent-permission-' + id);
            $(this).children('.glyphicon').removeClass('glyphicon-chevron-right').addClass('glyphicon-chevron-down');
            subSelector.removeClass('hide');
        }, function () {
            var id = $(this).data('id'), subSelector = $('.parent-permission-' + id);
            $(this).children('.glyphicon').removeClass('glyphicon-chevron-down').addClass('glyphicon-chevron-right');
            subSelector.addClass('hide');
        });

        $(".permission-delete").click(function () {
            Rbac.ajax.delete({
                type:'delete',
                data:{"_token" : "{{ csrf_token() }}" },
                confirmTitle: '确定删除该权限吗？请确保该权限没有关联的下级权限！',
                href: $(this).data('href'),
                successTitle: '权限删除成功',
                errorFnc:function(data){
                      swal('删除失败！', '', 'error');
                }
            });
        });

        $(".deleteall").click(function () {
            Rbac.ajax.deleteAll({
                confirmTitle: '确定删除选中的权限吗？如果该权限有下属权限将被一起删除！',
                href: $(this).data('href'),
                successTitle: '权限删除成功'
            });
        });

        
    </script>

@endsection